import os

import pandas as pd

from src.generator import Generator
from src.market_outcome import market_outcome
from src.run_market_clearing import run_market_clearing

if __name__ == "__main__":
    data_dir = os.path.join(os.path.dirname(__file__), "..", "data")
    file_name = "test.xlsx"
    file_path = os.path.join(data_dir, file_name)

    # 1. import data
    # read generator data from excel files
    generators = []
    df_generator = pd.read_excel(file_path, sheet_name="Generators", index_col=None)
    for row_idx, row in df_generator.iterrows():
        generator = Generator(row)
        generators.append(generator)
    # read load and time steps data
    df_load = pd.read_excel(file_path, sheet_name="Load", index_col=None)
    loads = df_load["Load"]
    time_steps = df_load["Time"].to_list()  # array to list

    # 2. run the market clearing problem
    m = run_market_clearing(generators=generators,
                            loads=loads,
                            time_steps=time_steps)

    # 3. output results into csv files
    results_dir = os.path.join(os.path.dirname(__file__), "..", "results")
    market_outcome(m, results_dir)
